<?php  
/** 
 * Thumbnail Generator for CakePHP that uses phpThumb (http://phpthumb.sourceforge.net/) 
 * 
 * @package default 
 * @author Nate Constant 
 **/  
 
App::uses('Component', 'Controller');
App::import('Vendor', 'phpThumb', array('file' => 'phpThumb/phpthumb.class.php')); 

class ThumbComponent extends Component{
     
    /** 
     * The mime types that are allowed for images 
     */ 
    var $allowed_mime_types = array('image/jpg','image/jpeg','image/pjpeg','image/gif','image/png'); 
    var $allowed_ext = array('jpg','jpeg','png','gif');
     
    /** 
     * File system location to save thumbnail to.  ** Must be writable by webserver  
     */ 
    // var $image_location = 'img/thumb'; 
     
    /** 
     * Array of errors 
     */ 
    var $errors = array(); 
     
    /** 
     * Default width if not set 
     */ 
    var $width = 200; 
     
    /** 
     * Default height if not set 
     */ 
    var $height = 200; 
     
    /** 
     * do we zoom crop the image? 
     */ 
    var $zoom_crop = 0;//do not zoom crop 
     
    /** 
     * The original image uploaded 
     * @access private 
     */ 
    var $file; 
     
    var $controller; 
    var $model; 
     
    function startup(Controller $controller ) { 
      $this->controller = &$controller; 
    } 

     
    /** 
     * This is the method that actually does the thumbnail generation by setting up  
     * the parameters and calling phpThumb 
     * 
     * @return bool Success? 
     * @author Nate Constant 
     **/ 
    function generateThumb($path, $filename,$tipo){ 
        // Make sure we have the name of the uploaded file and that the Model is specified 
        if(empty($filename)){ 
            throw new Exception(__("Nesusn file specificato.")); 
        } 
        // verify that our file is one of the valid mime types 
        if(!in_array(strtolower($tipo),$this->allowed_mime_types) && !in_array(strtolower($tipo),$this->allowed_ext)){ 
            // throw new Exception(__("Tipo file invalido.")); 
			return;
        } 
        // verify that the filesystem is writable, if not add an error to the object 
        // dont fail if not and let phpThumb try anyway 
        // if(!is_writable(WWW_ROOT.DS.$path)){ 
        if(!is_writable($path)){ 
            throw new Exception(__("La cartella %s non è scrivibile.", $path)); 
        } 
         
        // Load phpThumb 
        // vendor('phpThumb'.DS.'phpthumb.class'); 
        $phpThumb = new phpthumb();
        $phpThumb->setSourceFilename($path.DS.$filename); 
        $phpThumb->setParameter('w',$this->width); 
        // $phpThumb->setParameter('h',$this->height); 
        $phpThumb->setParameter('zc',$this->zoom_crop); 
        if($phpThumb->generateThumbnail()){ 
            if(!$phpThumb->RenderToFile($path.DS."thumb_".$filename)){
				CakeLog::error("Render del file ".$path.DS."thump_".$filename." fallito: " . implode(">", $phpThumb->debugmessages));
            	throw new Exception(__("Render del file %s fallito.",$path.DS."thump_".$filename)); 
            } 
        } else {
			CakeLog::error(__("Non è stato possibile generare thumbnail.") . implode(">", $phpThumb->debugmessages));
            throw new Exception(__("Non è stato possibile generare thumbnail.")); 
        } 
    }

} 
?>